package ru.acme.lab12.domain.client;

import ru.acme.lab12.domain.account.Account;
import ru.acme.lab12.domain.account.ImmutableAccount;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Client implements ImmutableClient {

	private List<Account> accounts;
	private String name;
	private Gender gender;
	
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((accounts == null) ? 0 : accounts.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((gender == null) ? 0 : gender.hashCode());
		return result;
	}

	public String toString() {
		StringBuffer stringBuffer = new StringBuffer('[');
		stringBuffer.append( "Client: name=" + name + ", gender="	+ gender + ", accounts: ");
		for (Account account : accounts) {
			stringBuffer.append('(');
			stringBuffer.append(account.toString());
			stringBuffer.append("), ");
		}
		stringBuffer.delete(stringBuffer.length() - 2, stringBuffer.length());
		return stringBuffer.toString();
	}

    // XXX [MY] Compare business keys enough in this case
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Client other = (Client) obj;
		if (accounts == null) {
			if (other.accounts != null)
				return false;
		} else if (!accounts.equals(other.accounts))
			return false;
		if (gender != other.gender)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	public Client(String name, Gender gender, Account account) {
		this.name = name;
		this.gender = gender;
		accounts = new ArrayList<Account>();
		accounts.add(account);
	}

	public List<? extends ImmutableAccount> getImmutableAccounts() {
		return Collections.unmodifiableList(accounts);
	}

	public List<Account> getAccounts() {
		return Collections.unmodifiableList(accounts);
	}

	public String getName() {
		return name;
	}

	public Gender getGender() {
		return gender;
	}
	
	public String getClientSalutation() {
		return gender.getSalutation() + " " + name;
	}
	
	public void addAccount(Account account) {
		accounts.add(account);
	}
}
